//后台管理专用Js
//版本:V1.10
/*******************************上传功能专用**********************************/
function OnUploadCompleted(err,url,file,message){
	if(url==''){alert('请正确选择文件上传!\n'+message);return};
    if(err==0){
    	alert('上传成功('+url+')!!       ');
        TM.BrowserFile();
    }else{
    	alert('上传失败!!\n\n原因:'+message+'.      ');
	}
}
// JavaScript Document
treeMenu = Class.create();
treeMenu.prototype.initialize = function(treeid, opts){
	this.id = treeid;
	opts = (opts==null)?{}:opts;
	this.nodeTagName = opts.tagName||'a';
	this.cssOpened = opts.cssOpened||'Opened';
	this.cssClosed = opts.cssClosed||'Closed';
	this.cssChild = opts.cssChild||'Child';
	this.ImgUrl = opts.ImgUrl || "/images/tree/s.gif";
	this.initMenu();
}
treeMenu.prototype.initMenu = function(){
	this.AllNodes = $(this.id).getElementsByTagName(this.nodeTagName);

	this.ImgBlankA ="<img src=\""+this.ImgUrl+"\" class=\"s\" onclick=\"ExCls(this,'"+this.cssOpened+"','"+this.cssClosed+"',1);\" alt=\"展开/折叠\" />";
	this.ImgBlankB ="<img src=\""+this.ImgUrl+"\" class=\"s\" />";
	
	for (i=0;i<this.AllNodes.length;i++ ) {
		var o  = this.AllNodes[i].parentNode;
		var nodeImg = document.createElement('img')
		nodeImg.src = this.ImgUrl;
		nodeImg.className = 's';
		nodeImg.alt = i;
		o.className==""?o.className=this.cssClosed:"";
		
		
		Event.observe(this.AllNodes[i],'click',this.selectedNode.bind(this,this.AllNodes[i]));
		
		if(Element.hasClassName(o,this.cssClosed)){
			Event.observe(nodeImg,'click',this.extendNode.bind(this,nodeImg));
		}
		Insertion.Top(o,nodeImg);

	}
}
treeMenu.prototype.selectedNode = function(o,e){
	//o=o.parentNode;
	Event.stop(e);
	
	for (i=0;i<this.AllNodes.length;i++ ) {
		if(this.AllNodes[i]==o){
			Element.addClassName(this.AllNodes[i],'current')
		}else{
			Element.removeClassName(this.AllNodes[i],'current');
		}
	}
}
treeMenu.prototype.extendNode = function(o,e){
	
	Event.stop(e);
	o=o.parentNode;
	
	if(Element.hasClassName(o,this.cssOpened)){
		Element.removeClassName(o,this.cssOpened);
		Element.addClassName(o,this.cssClosed);
	}else{
		Element.removeClassName(o,this.cssClosed);
		Element.addClassName(o,this.cssOpened);
	}
}
treeMenu.prototype.extendAllNode = function(){
	this.SetNodes(0);
}
treeMenu.prototype.setNodes = function (n) {
	var sClsName=n==0?this.cssOpened:this.cssClosed;
	for (i=0;i<this.AllNodes.length;i++ ) {
		Element.hasClassName(this.AllNodes[i],this.cssChild)?"":Element.addClassName(this.AllNodes[i],sClsName);
	}
}

var TabNumber = 100;
TabManage = Class.create();
TabManage.prototype.initialize = function(tabid, opts){
	this.tabElement = tabid;
	this.tabSelectedClass = "current";
	this.tabHoverClass = "hover";
	this.tabButtonsClass = 'TabButtons';
	this.isAlert = true;
	this.buttonElement = null;
	this.hasFocus = false;
	this.enableKeyboardNavigation = true;
	this.nextPanelKeyCode = TabManage.KEY_RIGHT;
	this.previousPanelKeyCode =TabManage.KEY_LEFT;
	
	opts = (opts==null)?{}:opts;
	
	this.isStopScroll = false;
	this.isConfirmClose = opts.isConfirmClose || false;
	this.scrollID = -1;
	this.scrollPx  = opts.scrollPx || 10;
	this.scrollSecond = opts.scrollSecond || 0.02 ;
	this.scrollElement = opts.scrollElement || 'tabScroll';
	this.sortableElement = opts.sortableElement || 'sortableTabs';
	this.LastElement = opts.LastElement || 'Last';
	this.templates = [];
	this.fckeditors = [];
	//this.contents = [];
	this.selectedIndex = opts.selectedIndex || 0;

	this.initTabs();
	this.initScrollEvent();
	this.addEventListener();
	
}
/*加载选项卡项*/
TabManage.prototype.initTabs = function(){
	$$('#'+this.tabElement+' .TabGroup .Tab').each(function(item){	
		this.initTabEvent(item);
		var title = item.innerHTML.stripTags().strip();
		var fname = (item.title!=null&&item.title.length>0)?item.title:title;
		this.templates[this.templates.length] = {element:item,title:title,name:fname,query:'',page:1,parentIndex:-1,isLoaded:false,content:null,number:TabNumber++};
	}.bind(this));
	var i=0; 
	$$('#'+this.tabElement+' .ContentGroup .Content').each(function(item){
		this.templates[i++].content = item;
	}.bind(this));
	this.selectTab(this.selectedIndex);
	
}
TabManage.prototype.addEventListener = function(){
	Event.observe(window,'resize',function(){this.setTabsWidth()}.bind(this));
	
}
TabManage.prototype.swithClass = function(obj,oldClass,newClass){
	for(var i=0;i<$(obj).parentNode.childNodes.length;i++){
		var item = $(obj).parentNode.childNodes[i];
		if(item==obj){
			item.className = newClass;
		}else{
			item.className = oldClass;
		}
	}
}
TabManage.prototype.initTabEvent = function (el){
	Element.setStyle(el,{cursor:'default'});
	el.onclick = this.onTabClick.bind(this,el);
	el.onfoucs = function(){this.blur();}
	el.onselectstart = function(){return false}
	el.ondblclick = this.closeTab.bind(this,el);
}

TabManage.prototype.selectTab = function(selectedIndex){
	if(selectedIndex>=this.templates.length){
		return ;
	}
	this.selectedIndex = selectedIndex;
	
	if(!this.templates[selectedIndex].isLoaded){
		this.tList();
	}else{
		this.showTab(this.templates[this.selectedIndex].element);
	}
}
/*取得索引号*/
TabManage.prototype.showTab = function(el,isReload){
	for(var i=0;i<this.templates.length;i++){
		if(this.templates[i].element == el){
			Element.addClassName(this.templates[i].element,'current');
			//Element.show(this.templates[i].content);
			Effect.Appear(this.templates[this.selectedIndex].content,{duration:0.5})
			//Effect.Grow(this.templates[i].content,{direction:'top-left',duration:0.3});
			
		}else{
			Element.removeClassName(this.templates[i].element,'current');
			Element.hide(this.templates[i].content);
		}			 
	}
}
/*取得索引号*/
TabManage.prototype.getIndexfromElement=function(el){
	for(var i=0;i<this.templates.length;i++){
		if(this.templates[i].element==el)
		return i;
	}
	return -1;
}

TabManage.prototype.onTabClick = function(el,e){
	if(el&&Element.visible(el))
		this.selectTab(this.getIndexfromElement(el));
	Event.stop(e);
}

TabManage.prototype.setTabsWidth = function(){
	var totalWidth = 80;
	for(var i=0;i<this.templates.length;i++){
		if(this.templates[i]!=null&&this.templates[i].element!=null)
		totalWidth += Element.getWidth(this.templates[i].element)+5;
	}

	Element.setStyle(this.templates[this.selectedIndex].element.parentNode,
					{width:totalWidth+'px'});
	var scrollWidth =0;
	scrollWidth = Element.getWidth(this.tabElement)-Element.getWidth(this.LastElement) -15;
	Element.setStyle(this.scrollElement,
					 {width:scrollWidth+'px'});
	this.initScrollButttonsStatus(scrollWidth);
	return totalWidth;
}

TabManage.prototype.initScrollEvent = function(){
	$$('#'+this.tabElement+' .ctrl').each(function(item){
		if(item.hasClassName('scrollLeft')){
				item.onmousedown = this.tabScroll.bind(this,'left');
				item.onmouseup = this.cancelScroll.bind(this);				
		}
		if(item.hasClassName('scrollRight')){
			item.onmousedown = this.tabScroll.bind(this,'right');
			item.onmouseup = this.cancelScroll.bind(this);
		}
		}.bind(this));
	this.initScrollButttonsStatus();
}
TabManage.prototype.initScrollButttonsStatus = function(totalWidth){
	$$('#'+this.tabElement+' .ctrl').each(function(item){
		if(item.hasClassName('scrollLeft')){
			if($(this.scrollElement).scrollLeft<=0){
				item.addClassName('disabled');
			}else{
				item.removeClassName('disabled');
			}
		}
		if(item.hasClassName('scrollRight')){
			var scrollWidth = $(this.scrollElement).scrollWidth - Element.getWidth(this.scrollElement)
			
			if(scrollWidth<=0 || $(this.scrollElement).scrollLeft>=scrollWidth){
				item.addClassName('disabled');
			}else{
				item.removeClassName('disabled');
			}
		}
		}.bind(this));
}
TabManage.prototype.openTab =function(fName,query,title){
	
	var selected = this.templates[this.selectedIndex];
	var title = (title!=null)?title:fName;
	var tab = document.createElement('li');
	Element.update(tab,'<span class="title">'+title+'</span>')
	this.initTabEvent(tab);
	var tabClose = document.createElement('a');
	tabClose.href='javascript:void(0)'
	Element.addClassName(tabClose,'close');
	Event.observe(tabClose,'click',function(tab,title,e){
			Event.stop(e);
			if(window.confirm('确定关闭('+title+')？ ')){
				this.closeTab(tab);
				return false;
				}
		}.bind(this,tab,title));
		
	Insertion.Bottom(tab,tabClose);
	Insertion.After(selected.element,tab);
	var content = document.createElement('div');

	Insertion.After(selected.content,content);
	Element.addClassName(content,'Content')
	var newtemplate = {element:tab,title:title,name:fName.strip(),query:query,page:1,parentIndex:this.selectedIndex,isLoaded:false,content:content,number:TabNumber++};

	var index = this.templates.length;

	this.templates[index] = newtemplate;

	this.selectedIndex = index;
	

	this.setTabsWidth();
	//Sortable.create(this.templates[this.selectedIndex].element.parentNode,{overlap:'horizontal'});
	this.tList();
}
TabManage.prototype.hasChildTab = function(index){
	for(var i=0;i<this.templates.length;i++){
		if(this.templates[i].parentIndex==index)
		return true;
	}
	return false;
}
/*关闭*/
TabManage.prototype.closeTab =function(el){
	if(el!=null){
		this.selectedIndex = this.getIndexfromElement(el);
	}
	if(this.templates[this.selectedIndex].parentIndex==-1) return;
	
	if(this.hasChildTab(this.selectedIndex)){
		if(!window.confirm('此选项卡含有子选项卡，关闭此将关闭所有全部子选项卡,\n是否确认关闭？'))return ;
		for(var i=0;i<this.templates.length;i++){
			if(this.templates[i].parentIndex==this.selectedIndex){
				this.closeTab(this.templates[i].element);
			}
		}
	}
	
	var parentIndex = this.templates[this.selectedIndex].parentIndex;
	Element.hide(this.templates[this.selectedIndex].element);
	Element.remove(this.templates[this.selectedIndex].element);
	Element.remove(this.templates[this.selectedIndex].content);
	this.templates = this.templates.without(this.templates[this.selectedIndex]);
	//this.templates[this.selectedIndex] = null;
	//加载父级标签
	this.selectTab(parentIndex);
}
TabManage.prototype.closeOtherTab = function(selectedIndex){
	this.selectTab(selectedIndex);
	for(var i=0;i<this.templates.length;i++){
		  if(this.templates[i].parentIndex==this.selectedIndex){
			  this.closeTab(this.templates[i].element);
		  }
	}
}
TabManage.prototype.closeChildTab = function(selectedIndex){
	this.selectTab(selectedIndex);
	for(var i=0;i<this.templates.length;i++){
		  if(this.templates[i].parentIndex==selectedIndex){
			  this.closeTab(this.templates[i].element);
		  }
	}
}
TabManage.prototype.tabScroll=function(direction){
	window.clearInterval(this.scrollID);
	this.isStopScroll = false;
	this.scrollID = window.setInterval(function(direction){this.scroll(direction);}.bind(this,direction),this.scrollSecond*1000);
}
TabManage.prototype.cancelScroll = function(){
	this.isStopScroll = true;
}
TabManage.prototype.scroll = function(direction){
	this.initScrollButttonsStatus()
	if(this.isStopScroll){
		window.clearInterval(this.scrollID);
		return;
	}
	var px = this.scrollPx;

	if(direction.toUpperCase()=='LEFT')	px = -this.scrollPx;
	var tabScroll = $(this.scrollElement);
	tabScroll.scrollLeft +=px;
}
/*刷新*/
TabManage.prototype.reload =function(){
	this.tList();
}

/*显示列表*/
TabManage.prototype.tList = function(cPage,Query,bAsyn){
	
	if(cPage!=null) this.templates[this.selectedIndex].page = cPage;
	if(Query!=null) this.templates[this.selectedIndex].query = Query;
	
	var q = 'f='+encodeURI(this.templates[this.selectedIndex].name)+'&page='+this.templates[this.selectedIndex].page + '&number__='+this.templates[this.selectedIndex].number+'&' + this.templates[this.selectedIndex].query
	new Ajax.Request('/xmlhttp.html',{asynchronous:bAsyn&&true,evalScripts:true,parameters: q ,method: 'get',
					onCreate: function(){
						//Element.update(this.templates[this.selectedIndex].content,'Loadding...');
						this.showLoading(this.templates[this.selectedIndex].content);
					}.bind(this),
					onComplete: function(r) {
						this.hideLoading();
						Element.update(this.templates[this.selectedIndex].content,r.responseText);
						this.templates[this.selectedIndex].isLoaded = true;
					}.bind(this)
	});
	this.showTab(this.templates[this.selectedIndex].element);
}
TabManage.prototype.tQuery = function(fQuery,QueryText){
	this.tList(1,fQuery);
}
/* 返回上级数据 */
TabManage.prototype.pList = function(){
	var parentIndex = this.templates[this.selectedIndex].parentIndex;
	this.closeTab(this.templates[this.selectedIndex].element);

	this.selectedIndex = parentIndex;
	this.tList();
}
TabManage.prototype.CreateDraggables = function(parenEl,className){
	Sortable.create(parenEl,{treeTag:'div',tag:'ul'});
/*	if(Object.isString(parenEl)) parenEl = $(parenEl);
	var childs = parenEl.getElementsByTagName('ul');
	for(var i=0;i< childs.length;i++){
		var el = childs[i];
		if(Element.hasClassName(el,className)){
			new Draggable(el, { constraint: 'vertical', snop[]});
		}
	}*/
	
}

/*************************执行命令*****************************/

TabManage.prototype.doConfirmCmd = function(/*String*/fCmd,opts){
	opts = opts || { };
	var fTitle = opts.title || fCmd;
	if(!window.confirm('是否确认['+fTitle+']？'))
				return false;
		return this.doCmd(fCmd,opts);
	},
/*执行命令*/
TabManage.prototype.doCmd =function(/*String*/fCmd,opts){
	opts = opts || { };	
	if(fCmd.indexOf('删除')>-1){
		if(!window.confirm('执行['+fCmd+'],将删除数据(不可恢复)？'))
			return false;
	}
	var q = 'f='+encodeURI(fCmd)+'&'+(opts.query||'');
	this.IsAlert = opts.isAlert || false;
	var rtnValue = false;
	var myAjax = new Ajax.Request('/xmlhttp.html',{asynchronous:false,parameters: q ,method: 'get',
																onComplete:function(r){
																	/*处理返回的数据*/
																	rtnValue = this.doResult(r.responseText);
																}.bind(this)});
	return rtnValue;
}
	/*处理返回数据*/
TabManage.prototype.doResult = function(fC){
		if(!fC){alert("提交服务器操作失败,请联系管理员!!!     ");return false;}
		var code = fC.split(",")[0] ;		//返回的代码
		var message = fC.split(",")[1] ;	//返回的信息
		var value = fC.split(",")[2];		//附加值,用于表单验证,返回input 的ID
		try{
			switch (code){
				case '1':
				case '2':
					var info="操作成功!!!";
					if(message && message.length>0) info = message;
					//是否回显提示
					if(this.IsAlert) alert(message, '提示');
					return true;
					break;
				case '101':
					if(message){
						alert(message + "("+ code + ")");
					}else{
						alert(code);
					}
					if(value&& $(value)) $(value).focus();
					return false;
				case '102':
					alert('找不到此更新的内容！！！('+code+')');
					return false;
				case '103': 
					alert('验证码填写出错，请检查！！！('+code+')');
					return false;
					break;
				default:
					if(message){
						alert(message + "("+ code + ")");
					}else{
						alert(code);
					}
					return false;
			}
		}catch(e){alert(e.message);}
		return false;
}
/**********************************表单提交处理****************************************/
TabManage.prototype.saveContentCmd = function(fObj,tForm,isAlert){
		try{
			this.isAlert = isAlert;
			fObj.disabled=true;
			alert(tForm);
			var d = Form.serialize(tForm);
			alert(d);
			var myAjax = new Ajax.Request('/aspx/cityflypost.aspx',{
										  asynchronous:false, parameters: d ,method: 'post',
										  onComplete:function(r){
											  //处理返回的数据
										  		rtnValue = this.returnSaveCmdResult(r.responseText);
											}.bind(this)});
			fObj.disabled=false;
			return rtnValue;
		}catch(e){
			alert(e.message);
			fObj.disabled=false;
			return false;
		}
		return false;
}
/* 处理 提交数据后返回的代码和消息 */
TabManage.prototype.returnSaveCmdResult = function(rC){
		var arr = rC.split(',');
		switch(arr[0])
		{
			case "1":
				if(TM.isAlert) alert(arr[1],'提示');
				return true;
			case "2":
				if(TM.isAlert) alert(arr[1],'提示');
				return true;
			default:
				alert(arr[1]);
				break;
		}
		return false;
}

/*执行多选项命令*/
TabManage.prototype.doCheckBoxCmd = function(fName,fCb,fVal,balert){
	if(this.CheckIsChecked(fCb)){
		return this.doCmd(fName,fCb+'='+this.getCheckBoxValue(fCb)+'&State='+fVal,balert||false);
	}
}

TabManage.prototype.doCheckBoxConfirmCmd = function(fName,title,fCb,fVal,balert){
	if(this.CheckIsChecked(fCb)){
		return this.doConfirmCmd(fName,title,fCb+'='+this.getCheckBoxValue(fCb)+'&State='+fVal,balert||false);
	}
}

/*是否有选定选项 Key 为name属性*/
TabManage.prototype.CheckIsChecked = function(fId){
	var obj=this.templates[this.selectedIndex].content.getElementsByTagName("INPUT");
	for(var i=0;i<obj.length;i++)
			if(obj[i].type=="checkbox")
			{
				if(obj[i].id==fId||obj[i].name==fId)
				{
					if(obj[i].checked&&!obj[i].disabled)
					{
						return true;
					}
				}
			}
		window.alert("请至少选择一个");
	return false;
}
TabManage.prototype.getCheckBoxValue = function(fId){
		var str ="";
		var obj=this.templates[this.selectedIndex].content.getElementsByTagName("INPUT");
		for(var i=0;i<obj.length;i++)
			if(obj[i].type=="checkbox")
			{
				if(obj[i].id==fId||obj[i].name==fId)
				{
					if(obj[i].checked&&!obj[i].disabled)
					{
						str = str +  obj[i].value + ",";
					}
				}
			}
		return str;
}
	/* 复选框 全选 / 取消 /反选*/
TabManage.prototype.CheckBoxSelect = function(fId,flag)
	{
		var obj=this.templates[this.selectedIndex].content.getElementsByTagName("INPUT");
		for(var i=0;i<obj.length;i++)
			if(obj[i].type=="checkbox"&&!obj[i].disabled)
			{
				if((!fId)||obj[i].id==fId||obj[i].name==fId){
					if(flag==2)
					{
						obj[i].checked=!obj[i].checked;
					}else{
						obj[i].checked=flag;
					}
				}
			}
}
TabManage.prototype.openHtml = function(divid,htmlid,height){
	var oEditor = new FCKeditor(htmlid+this.selectedIndex,null,height,'Admin') ;
	oEditor.Value = $(htmlid).value;
	Element.update(divid,oEditor.CreateHtml());
    //oEditor.ReplaceTextarea() ;
}
TabManage.prototype.GetContents = function(htmlid){
	var oEditor = FCKeditorAPI.GetInstance(htmlid+this.selectedIndex); 
	
	$(htmlid).value = oEditor.GetXHTML(true);
}
TabManage.prototype.GetInnerText = function(textid,htmlid){
	var oEditor = FCKeditorAPI.GetInstance(htmlid+this.selectedIndex); 
	$(textid).value = oEditor.EditorDocument.body.innerText.replace(/\r\n/gi,'');
}
/* 加载内容 */
TabManage.prototype.loadContent = function(/*Object*/node,/*String*/fName,/*String*/query,bAsyn){

	var q = 'f='+encodeURI(fName)+'&'+query;
	new Ajax.Updater(node,'/xmlhttp.html',{asynchronous:bAsyn&&true,evalScripts:true,parameters: q ,method: 'get',onCreate: function(){
			this.showLoading();
	}.bind(this),onSuccess:this.hideLoading.bind(this)});
}
TabManage.prototype.showLoading = function(el){
	if(this.divLoading){
		Element.show(this.divLoading);
	}else{
		this.divLoading = document.createElement('div');
		Insertion.Top(document.body,this.divLoading);
		Element.setStyle(this.divLoading,{
			left:0,top:0,textIndent:'-500px',padding:'2px 155px 2px 6px',background:'#fff url(/images/common/loading_mini.gif) no-repeat 6px',position:'absolute'
			});
		this.divLoading.title ='数据加载中';
		this.divLoading.innerHTML ='数据加载中';
	}
}
TabManage.prototype.hideLoading = function(){
	if(this.divLoading) Element.hide(this.divLoading);
}
TabManage.prototype.moveOptions = function(obj1,obj2){
	var obj1 = $(obj1);
	var obj2 = $(obj2);
	for(var i=obj1.options.length-1;i>=0;i--){
		if(obj1[i].selected){
            var oOption =new Option(obj1[i].text,obj1[i].value);
			oOption.selected = true;
			obj2.options.add(oOption);
			obj1.options[i] = null;
		}
	}
}
TabManage.prototype.setOptionsSelected = function(obj){
	var obj = $(obj);
	for(var i=obj.options.length-1;i>=0;i--){
		obj[i].selected = true;
	}
}
TabManage.KEY_UP = 38;
TabManage.KEY_DOWN = 40;
TabManage.KEY_LEFT = 37;
TabManage.KEY_RIGHT = 39;



/**
 * 此方法实现了:日期时间的选择功能,如果有传返回日期格式编号，
 * 则参照下列给定的格式返回时间格式，可以修改setDay()函数扩展返回日期格式
 * 
 * @param string formElt 填写时间域唯一名称或id值字符串
 * @param Object e 事件对像Event
 * @param string format 返回日期格式（选填）：格式化分3类，可自已扩展。只是要返回YYYY-mm-dd的日期格式，无须填写此参数
 * 										10～20：之间代表只有日期格式的返回字符串例：
 * 														0: 代表返回YYYY-mm-dd
 * 														11：代表返回YYYY年mm月dd日
 * 														12：返回YYYY/mm/dd
 * 										20～30：之间代表只有日期格式的返回字符串例：
 * 														21：YYYY-mm-dd HH:ii:ss
 * 														22：YYYY年mm月dd日 HH时ii分ss秒
 * @return 如果没有传入格式化编号默认返回YYYY-mm-dd的日期格式，否则按format编号返回日期格式
 * @version 1.0
 */
var DP_;
function getDatePicker(el,format) {
		DP_ = new DatePicker(el,{format:format});
}
DatePicker = Class.create();
DatePicker.prototype.initialize = function(el,opts){
	this.formEl = $(el);
	this.initOptions(opts);
	this.initContainer();
	this.setDateTime(this.formEl.value);
	this.setLayout();
	this.show();
}
DatePicker.prototype.initOptions = function(opts){
	opts = opts || {};
	this.timeFormat = opts.format || 0;
	this.divId = opts.divId || 'DateTime_Picker';
	this.timeSwitch =20;
	this.months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月","八月", "九月", "十月", "十一月", "十二月");
	this.imageUrl='/images/common/calendar/'
}
DatePicker.prototype.initContainer = function(){
	this.container = $(this.divId);
	if(!this.container){
		this.container = document.createElement("div");
		this.container.id = this.divId;
		Element.setStyle(this.container,{
			overflow :'hidden',
			position:'absolute',
			backgroundColor :'#fff',
			display : 'none',
			border : '1px solid #bbb',
			paddingBottom:'6px',
			width: '200px',
			zIndex:'999'
		});
		Insertion.After(this.formEl,this.container);
	}
	//var height = Element.getDimensions(this.formEl).height;
	
}
DatePicker.prototype.setDateTime = function(){
	if(this.timeFormat < this.timeSwitch) {
		Event.observe(this.formEl,'blur',this.hide.bind(this)); 	//含有时间格式时加上鼠标离开层时隐藏事件
	}
	var dateValue = this.formEl.value;
	var time;
	if(dateValue&&dateValue!='') {
		var re=new RegExp("-","g"); 
		dateValue=dateValue.replace(re,'/');
		time=new Date(dateValue);
	}
	if(!time || time=='NaN')time=new Date();
	
	this.month = time.getMonth();
	this.year = time.getFullYear();
	this.value = time;
}
DatePicker.prototype.show = function(){
	var date;
	//判断目标域是否有值
	if(typeof this.value == "object" ) {
		date = this.value;
	} else {
		date=new Date();
	}
		
	var isThisYear = (this.year == date.getFullYear());
	var isThisMonth = (isThisYear && this.month == date.getMonth());
	var today=(isThisYear && isThisMonth ? date.getDate():-1); 
	this.hour = date.getHours();
	this.second = date.getSeconds();
	this.minutes = date.getMinutes();

	var html="<table id=tablePicker width=100% cellspacing=0 cellpadding=0 borderColorLight=#FFBE00 borderColorDark=#FFBE00 border=0 bgcolor=#ffffff style='font-size:12px;font-family:宋体;table-layout:fixed'>";
	html+="<tr height=20px borderColor=#FFBE00 bgColor=#eeeeee align=center><td colSpan=7 style='padding-top:3px;cursor:move'>";

	//display year
	html+="<font style=\"cursor:pointer\" onmousedown=\"DP_.incYear(-1)\" color=white><img src=\""+this.imageUrl+"/back.gif\" /></font>&nbsp";
	html+="<font color=" + (isThisYear ? "green":"#ED1C24") + ">" + this.year + "年</font>";
	html+="&nbsp<font style='cursor:pointer' onmousedown=\"DP_.incYear(1)\" color=white><img src=\""+this.imageUrl+"/next.gif\" /></font>&nbsp;&nbsp;&nbsp;";

	//display month
	html+="<font style=\"cursor:pointer\" onmousedown=\"DP_.incMonth(-1)\" color=white><img src=\""+this.imageUrl+"/back.gif\" /></font>&nbsp";
	if(this.month<10)html+="&nbsp";
	html+="<font color=" + (isThisMonth ? "green":"#ED1C24") + ">" + this.months[this.month] + "</font>";
	if(this.month<10)html+="&nbsp";
	html+="&nbsp<font style=\"cursor:pointer\" onmousedown=\"DP_.incMonth(1)\" color=white><img src=\""+this.imageUrl+"/next.gif\" /></font>";

	//目标输入框为只读时显示清除按钮
	if(this.formEl.readOnly) {
		//判断输入框是否为只读，如果是的话，显示清除按钮
		html+="&nbsp;<span onclick='formElement.value=\"\";DP_.hide();' style=\"cursor:pointer;\"><img src=\""+this.imageUrl+"/btn_clear.gif\" /></span>";	
	}
	if(this.timeFormat > this.timeSwitch) {
		html+="&nbsp;<span onclick='DP_.hide();' style=\"cursor:pointer;\"><img src=\""+this.imageUrl+"/btn_close1.gif\" /></span>";	
	}

	html+="</td></tr>";
	//display day
	html+="<tr height=\"20px\" align=\"center\"><td style=\"padding-top:3px;BORDER-LEFT:none;BORDER-RIGHT:none\"><font color=\"#ED1C24\">日</font></td><td class=week>一</td><td class=week>二</td><td class=week>三</td><td class=week>四</td><td class=week>五</td><td class=week><font color=\"green\">六</font></td></tr>";

	var startDay=new Date(this.year,this.month,1).getDay();
	var dayOfMonthOfFirstSunday = (7 - startDay + 1);
	var intDaysInMonth=this.getDays(this.month, this.year);
	
	for (var intWeek = 0; intWeek < 6; intWeek++) {
		html+="<tr height=18px align=center>";
		var dayOfMonth;
		var tdnum = 0;
	    for (var intDay = 0; intDay < 7; intDay++) {
	     	dayOfMonth = (intWeek * 7) + intDay + dayOfMonthOfFirstSunday - 7;
		 	if (dayOfMonth <= 0) {
		 			tdnum++;
	          		html+="<td height=18px borderColor=#ffffff>&nbsp</td>";
			} else if(dayOfMonth <= intDaysInMonth) {
				tdnum++;
		   		html+="<td borderColor=#ffffff "+(dayOfMonth==today ? "bgcolor=#ffcc66":"")+" onmousedown=\"DP_.select(" + dayOfMonth + ",event)\"";
		   		html+="style=\"cursor:pointer;color:" + (dayOfMonth==today ? "red":"black") + "\">" + dayOfMonth + "</td>";
			}
		}
		
		if(intWeek+1==6 && this.timeFormat > this.timeSwitch) {
			if(tdnum != 0 && tdnum != 7) {
				html+="</tr><tr height=18px align=center>";
			}
			html+="<td colspan='7' borderColor=#ffffff><div style='margin:-1.5px;'><input maxlength='2' onmousedown='this.select()' name='hour_' id='hour_' style='border: 1px solid #CCCCCC;font-size:12px;font-family:宋体;line-height:12px;height:12px;width:20px;text-align:center;padding:2px;' size='1' value='"+ (this.hour<10?('0'+this.hour):this.hour) +"' type='text'> 时 <input maxlength='2' onmousedown='this.select()' value='"+ (this.minutes<10?('0'+this.minutes):this.minutes) +"' style='border: 1px solid #CCCCCC;font-size:12px;font-family:宋体;line-height:12px;height:12px;text-align:center;width:20px;padding:2px;' id='minutes_' name='minutes_' size='1' type='text'> 分 <input maxlength='2' onmousedown='this.select()' name='second_'  id='second_' style='border: 1px solid #CCCCCC;font-size:12px;line-height:12px;height:12px;text-align:center;width:20px;padding:2px;' value='"+ (this.second<10?('0'+this.second):this.second) +"' size='1' type='text'> 秒</div></td>";
		}
		html+="</tr>";
		
	}
	html+="</table>";
	Element.update(this.container,html);
	Element.show(this.container);
	
}
DatePicker.prototype.setLayout = function(){
	var p =Element.positionedOffset(this.formEl);
	var height = Element.getDimensions(this.formEl).height;
	Element.setStyle(this.container,{
		left:p.left+'px',
		top:p.top+height+1+'px'
		})
}

DatePicker.prototype.hide = function(e){
	Element.hide(this.container);
	Event.stop(e);
}

DatePicker.prototype.getDays = function(month, year) {
	if (1==month)return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
	var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31,30, 31, 30, 31);
	return daysInMonth[month];
}

DatePicker.prototype.select = function(day,e) {
	//整理年月日时分秒
	var y,m,d,h,i,s;
	y = this.year;
	m = (this.month<9 ? "0":"") + (this.month+1);
	d = (day<10 ? "0":"") + day;
	if(this.timeFormat && this.timeFormat > this.timeSwitch) {
		re = /[^\d]/g;            // 创建正则表达式模式。
		hour = $("hour_");
		minutes = $("minutes_");
		second = $("second_");
		if(re.test(hour.value) || Math.abs(parseInt(hour.value))>23){
			alert("时间出错：小时只能是0～23的整数");
			this.hour.focus();
			this.hour.select();
			return false;
		}
		if(re.test(minutes.value) || Math.abs(parseInt(minutes.value))>59){
			alert("时间出错：分钟只能是0～59的整数");
			this.minutes.focus();
			this.minutes.select();
			return false;
		}
		if(re.test(second.value) || Math.abs(parseInt(second.value))>59){
			alert("时间出错：分钟只能是0～59的整数");
			this.second.focus();
			this.second.select();
			return false;
		}
		
		h = (hour.value.length < 2 ? "0" : "") + hour.value;
		i = (minutes.value.length < 2 ? "0":"") + minutes.value;
		s = (second.value.length < 2 ? "0":"") + second.value;
	}
	var formEV="";

	//根据要返回的数据类型组合数据
	if(this.timeFormat == 0) {
		formEV = y + '-' + m + '-' + d;
	} else if(this.timeFormat == 11) {
		formEV = y + '年' + m + '月' + d + '日';
	} else if(this.timeFormat == 12) {
		formEV = y + '/' + m + '/' + d;
	} else if(this.timeFormat == 21) {
		formEV = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
	} else if(this.timeFormat == 22) {
		formEV = y + '年' + m + '月' + d + '日 ' + h + '时' + i + '分' + s + '秒';
	}
	this.formEl.value = formEV;

	this.formEl.focus();
	this.hide();
	Event.stop(e);
}
/** 月份增、减操作 **/
DatePicker.prototype.incMonth = function(delta) {
	this.month+=delta;
	if (this.month>=12) {
		this.month = 0;
		this.incYear(1);
	} else if(this.month<=-1) {
	     this.month = 11;
	     this.incYear(-1);
	} else {
		this.show();
	}
}
/** 年份增、减操作 **/
DatePicker.prototype.incYear = function(delta,eltName) {
	this.year+=delta;
	this.show();
}



/**
 * 实现层的拖曳操作
 */
function drag(o){
    o.onmousedown = function(a){
        var d = document;
        if(!a) a = window.event;
        if(o.setCapture)
            o.setCapture();
        else if(window.captureEvents)
            window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

		var re = new RegExp("px","ig");
		//计算当前鼠标的坐标点离日期采集器层的零点坐标的Ｘ间距
        var x = (a.pageX?a.pageX:a.x) - parseInt(o.style.left.replace(re,""));
        //计算当前鼠标的坐标点离日期采集器层的零点坐标的Y间距
        var y = (a.pageY?a.pageY:a.y) - parseInt(o.style.top.replace(re,""));
        
        d.onmousemove = function(a){
            if(!a)a=window.event;
            if(!a.pageX)a.pageX=a.clientX;
            if(!a.pageY)a.pageY=a.clientY;
            var tx=a.pageX-x,ty=a.pageY-y;
            o.style.left = tx + "px";
            o.style.top = ty + "px";
        };
        
        d.onmouseup=function(){
            if(o.releaseCapture)
                o.releaseCapture();
            else if(window.captureEvents)
                window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
            d.onmousemove=null;
            d.onmouseup=null;
        };
    };
}